#include "netpcap_intf.h"
#include "netpcap.h"

#ifdef __cplusplus
extern "C" {
#endif

using namespace secsmart_pcap;

pcapmgr_ptr pcapmgr_new()
{
    netpcap_mgr *pcapmgr = new netpcap_mgr;
    pcapmgr->init(nullptr);
    return pcapmgr;
}

void pcapmgr_start_capture(pcapmgr_ptr pcapmgr, pcap_intf_param_t *param)
{
    if (pcapmgr == nullptr || param == nullptr) {
        return;
    }
    
    cap_task_t task;
    if (param->task_name != nullptr) {
        task.task_info.task_name = param->task_name;
    }
    if (param->if_name != nullptr) {
        task.task_info.if_name = param->if_name;
    }
    if (param->filter != nullptr) {
        task.task_info.filter_string = param->filter;
    }
    task.task_info.read_file = param->replay_file;
    task.task_info.write_file = param->write_file;
    if (param->file_dir != nullptr) {
        task.file_info.dir = param->file_dir;
    }
    task.fn_ctx = param->ctx;
    task.fn_pf_handler = param->pkt_handler;
    task.fn_pf_stoper = param->stop_handler;

    if (param->replay_file) {
        ((netpcap_mgr *)pcapmgr)->pcap_task_start_replay(task);
    } else {
        ((netpcap_mgr *)pcapmgr)->pcap_task_start(task);
    }
}

void pcapmgr_stop_capture(pcapmgr_ptr pcapmgr, char *task_name)
{
    if (pcapmgr == nullptr || task_name == nullptr) {
        return;
    }
    ((netpcap_mgr *)pcapmgr)->pcap_task_stop(task_name);
}

void pcapmgr_stop_all_capture(pcapmgr_ptr pcapmgr)
{
    if (pcapmgr == nullptr) {
        return;
    }
    ((netpcap_mgr *)pcapmgr)->pcap_task_stop_all();
}

void pcapmgr_log_set_level(pcapmgr_ptr pcapmgr, uint32_t level, bool trace_func)
{
    ((netpcap_mgr *)pcapmgr)->log_ctrl(level, trace_func);
}

#ifdef __cplusplus
}
#endif